﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace SaaS
{
    /// <summary>
    /// Summary description for AccountService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class AccountService : System.Web.Services.WebService
    {

        [WebMethod]
        public string user_info_parse(string username, string password)
        {
            int valid = 1;
            string newUser ="";
            newUser = valid.ToString() + "|" + username + "|" + password;

            return newUser;
        }

        [WebMethod]
        public string User_Login(string newUser)
        {
            int success = 0;
            int idx = newUser.IndexOf("|");
            string valid = newUser.Substring(0, idx);
            int idx2 = newUser.LastIndexOf("|");
            string username = newUser.Substring(idx + 1, idx2 - idx-1);
            string password = newUser.Substring(idx2+1);

            if (valid == "1")
            {

                MiniSaaSDB db = new MiniSaaSDB();
                db.open();
                if (db.isOpen())
                {
                    string sql = "select * from LOGIN where Login_Name ='" + username + "' and Login_Password = '" + password + "';";
                    System.Data.SqlClient.SqlDataReader reader = db.executeQuery(sql);
                    if (reader.HasRows)
                        success = 1;
                    else
                        success = 0;
                    reader.Close();
                    db.close();
                }
            }
            newUser = success.ToString() + "|" + username + "|" + password;
            return newUser;
        }
        [WebMethod]
        public string Confirmation_Page(string input)
        {
            string success;
            if (input.IndexOf("|") >= 0)
            {
                success =  input.Substring(0, input.IndexOf("|"));
            }
            else
            {
                success = input;
            }
            
            if (success =="1")
                return "Welcome, Authorized User!";
            else
                return "Sorry, try again!";
        }
        [WebMethod]
        public string CheckID(string newUser)
        {
            int available = 0;

            int idx = newUser.IndexOf("|");
            string valid = newUser.Substring(0, idx);
            int idx2 = newUser.LastIndexOf("|");
            string username = newUser.Substring(idx + 1, idx2 - idx - 1);
            string password = newUser.Substring(idx2+1);

            if (valid == "1")
            {
                MiniSaaSDB db = new MiniSaaSDB();
                db.open();
                if (db.isOpen())
                {
                    string sql = "select Login_ID from LOGIN where Login_Name ='" + username + "';";
                    System.Data.SqlClient.SqlDataReader reader = db.executeQuery(sql);
                    if (reader.HasRows)
                        available = 0;
                    else
                        available = 1;
                    reader.Close();
                    db.close();
                }

           }
            newUser = available.ToString() + "|" + username + "|" + password;
            return newUser;

        }
        [WebMethod]
        public string checkPwd(string newUser)
        {

            int idx = newUser.IndexOf("|");
            string valid = newUser.Substring(0, idx);
            int idx2 = newUser.LastIndexOf("|");
            string username = newUser.Substring(idx + 1, idx2 - idx - 1);
            string password = newUser.Substring(idx2+1);

            int legal = 0;
            if (valid == "1")
            {
                
                //char[] letter = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
                //char[] number = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
                if (password.Length < 6)
                {
                    legal = 0;
                }
                else if (password.Length > 50)
                {
                    legal = 0;
                }
                /*else if (password.IndexOfAny(letter) == -1)
                {
                    legal = 0;
                }
                else if (password.IndexOfAny(number) == -1)
                {
                    legal = 0;
                }*/
                else
                    legal = 1;
                
            }
            newUser = legal.ToString() + "|" + username + "|" + password;
            return newUser;
        }
        [WebMethod]
        public string Register(string newUser)
        {
            int success = 0;

            int idx = newUser.IndexOf("|");
            string valid = newUser.Substring(0, idx);
            int idx2 = newUser.LastIndexOf("|");
            string username = newUser.Substring(idx + 1, idx2 - idx - 1);
            string password = newUser.Substring(idx2+1);

            if (valid == "1")//user name and pwd could be added
            {
                MiniSaaSDB db = new MiniSaaSDB();
                db.open();
                if (db.isOpen())
                {
                    int newID;
                    string sql = "select MAX(Login_ID)+1 from LOGIN";
                    System.Data.SqlClient.SqlDataReader reader = db.executeQuery(sql);
                    if (reader.HasRows)
                    {
                        reader.Read();
                        newID = (int)reader[0];
                    }
                    else
                    {
                        newID = 1;
                    }
                    reader.Close();
                    sql = "insert into LOGIN(Login_ID, Login_Name, Login_Password) Values (" + newID.ToString() + ",'" + username + "','" + password + "');";
                    int rows = db.executeNonQuery(sql);
                    if (rows <= 0)
                        success = 0;
                    else
                        success = 1;
                    db.close();
                }
            }
            newUser = success.ToString() + "|" + username + "|" + password;
            return newUser;
        }
    }
}
